using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.IO;
using LocalizeMe.Sources;
using System.Drawing.Design;

namespace LocalizeMe.ResourceTool.Dictionary
{

    [Serializable]
    class AssemblyItem
    {
        private string _name;
        [NonSerialized]
        private Collection<TypeItem> _items;
        private string _subFolder;
        private string _filePath;

        private string _languagePath;

        private ResourceSource _source = null;//new XmlSource(); //NullSource();

        [Editor(typeof(SourceEditor), typeof(UITypeEditor))]
        public ResourceSource Source
        {
            get
            {
                return _source;
            }
            set
            {
                _source = value;
            }
        }

        //[Editor(typeof(Iris.Common.Modules.Designers.UIPathSelection), typeof(UITypeEditor))]
        public string LanguagePath
        {
            get
            {
                return _languagePath;
            }
            set
            {
                _languagePath = value;
            }
        }

        public string FilePath
        {
            get
            {
                return _filePath;
            }
            set
            {
                _filePath = value;
            }
        }

        public string SubFolder
        {
            get
            {
                return _subFolder;
            }
            set
            {
                _subFolder = value;
                FileInfo fi = new FileInfo(this.FilePath);
                this.LanguagePath = Path.Combine(fi.DirectoryName, @"Languages\" + this.SubFolder + "\\");
            }
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }


        public Collection<TypeItem> Items
        {
            get
            {
                if (_items == null)
                    _items = new Collection<TypeItem>();
                return _items;
            }
        }

        private static PropertyItem AnalyseProperty(PropertyInfo property)
        {
            PropertyItem analysedProperty;


            analysedProperty = new PropertyItem();
            analysedProperty.Name = property.Name;

            MethodInfo[] infos = property.GetAccessors(false);
            if (infos != null && infos.Length > 0)
            {
                object[] objs = property.GetCustomAttributes(true);

                if (objs.Length > 0)
                {
                    bool _changed = false;
                    foreach (object obj in objs)
                    {
                        if (obj.GetType().FullName == "LocalizeMe.LocalDescriptionAttribute")
                        {
                            TranslationItem item = new TranslationItem();
                            item.Code = ((LocalDescriptionAttribute)obj).Key;
                            analysedProperty.Item = item;
                            _changed = true;
                            break;
                        }
                    }
                    if (!_changed)
                    {
                        TranslationItem item = new TranslationItem();
                        analysedProperty.Item = item;
                    }
                }
                else
                {
                    TranslationItem item = new TranslationItem();
                    analysedProperty.Item = item;
                }
            }

            return analysedProperty;
        }

        private static TypeItem AnalyseType(Type type)
        {
            TypeItem analysedType;


            analysedType = new TypeItem();
            analysedType.Name = type.Name;

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (type.FullName.Equals(property.DeclaringType.ToString()))
                {
                    analysedType.Properties.Add(AnalyseProperty(property));
                }
            }
            return analysedType;
        }

        public static AssemblyItem AnalyseAssembly(string filePath)
        {
            Assembly ass = Assembly.LoadFile(filePath);


            AssemblyItem analysedAssembly = null;

            if (ass != null)
            {
                analysedAssembly = new AssemblyItem();
                FileInfo fi = new FileInfo(filePath);
                analysedAssembly.Name = fi.Name;
                analysedAssembly.FilePath = fi.FullName;


                if (string.IsNullOrEmpty(analysedAssembly.SubFolder))
                    analysedAssembly.SubFolder = fi.Name.TrimEnd(fi.Extension.ToCharArray());

                analysedAssembly.SubFolder = analysedAssembly.SubFolder;


                Type[] types = ass.GetTypes();

                foreach (Type type in types)
                {
                    analysedAssembly.Items.Add(AnalyseType(type));
                }
            }
            return analysedAssembly;

        }
    }

}
